package com.compty.paint.freemarker;

import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.paint.domain.Painter;
import com.compty.paint.domain.support.MarketIndexEntity;
import com.compty.paint.freemarker.support.PainterComparator;
import com.compty.paint.manager.PainterManager;

import freemarker.template.TemplateException;

/**
 * @Todo:市场行情页生成
 * @author: 朱庆辉
 * @CreateTime:2012-3-26 下午01:51:01
 */
public class MarketIndexGenerate  extends BaseGenerate {
	
	private static final long serialVersionUID = 6839910199171408437L;
	
	private PainterManager painterManager;

	public MarketIndexGenerate() {
		painterManager = SpringContextHolder.getBean("painterManager");
	}

	public void generateHtml() throws TemplateException, IOException {
		
		 Comparator<Painter> ascComparator = new PainterComparator(); 
		 Comparator<Painter> descComparator = Collections.reverseOrder(ascComparator); 
		 
	     List<Painter> allList = painterManager.findAllPainerStar();
	     Collections.sort(allList, descComparator);
	     List<MarketIndexEntity> allMarketIndex = getMarketIndex(allList);
	     root.put("marketIndexList", allMarketIndex);
	     root.put("type", "");
	     output("market_index.ftl", "", "market_index.html");
		 
		 List<Painter> guohuaList = painterManager.findGuohuaPainerStar();
	     Collections.sort(guohuaList, descComparator);
	     List<MarketIndexEntity> guohuaMarketIndex =  getMarketIndex(guohuaList);
	     root.put("marketIndexList", guohuaMarketIndex);
	     root.put("type", "国画");
	     output("market_index.ftl", "", "market_index_gh.html");
	      
	     List<Painter> youhuaList = painterManager.findyouhuaPainerStar();
	     Collections.sort(youhuaList, descComparator);
	     List<MarketIndexEntity> youhuaMarketIndex = getMarketIndex(youhuaList);
	     root.put("marketIndexList", youhuaMarketIndex);
	     root.put("type", "油画");
	     output("market_index.ftl", "", "market_index_yh.html");
	     
	}
	
	private List<MarketIndexEntity>  getMarketIndex(List<Painter> list){
	      List<MarketIndexEntity> youhuaMarketIndex = new ArrayList<MarketIndexEntity>();
	      for (Painter painter : list) {
	   	   String name  = painter.getName();
				 
					 String price = "";
					 String time_period = "";
					 String upDownFlag = "";
					 String upDownRatio = "";
					 Long painterId = painter.getId();
					 if(painter.getNewestIndex() !=  null){
						 price = painter.getNewestIndex().getPrice();
						 price = price.trim();
						 String price_last = painter.getNewIndex().getPrice().trim();
						 time_period = painter.getNewestIndex().getTime_period();
						 if( painter.getNewIndex() != null){
							 int price_int = 0;
							 int price_last_int = 0;
							 try {
								price_int = Integer.parseInt(price) ;
								price_last_int = Integer.parseInt(price_last);
								if(price_int > price_last_int ){
									 upDownFlag = "up";
									 upDownRatio = doubleConvertToFraction((price_int - price_last_int)/Float.parseFloat(price_last));
								}else{
									 upDownFlag = "down";
									 upDownRatio = doubleConvertToFraction((price_int - price_last_int)/Float.parseFloat(price_last));
								}
							} catch (NumberFormatException e) {
								 upDownFlag = "up";
								 upDownRatio = "0.0";
							}
						 }
					 }
					 
					 if("".equals(price)){
						 continue;
					 }
					 
					 MarketIndexEntity indexEntity = new MarketIndexEntity(name, price,time_period,painterId);
					 indexEntity.setUpDownFlag(upDownFlag);
					 indexEntity.setUpDownRatio(upDownRatio);
					 youhuaMarketIndex.add(indexEntity);
		   }
	      
	      return youhuaMarketIndex;
	}
	
	private String doubleConvertToFraction(double number){
		
		 NumberFormat nf = NumberFormat.getPercentInstance();
		 nf.setMaximumFractionDigits(2);
		 
		 return nf.format(number);
	}

}
